package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.factory.SynchronousFactory;
import com.tbit.uqbike.constant.enums.SynchronousConfigEnum;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.baseAbstract.SynchronousService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 同步配置控制器
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-20
 */
@RestController
@RequestMapping("/syncConfig")
public class SyncConfigController {

    @Resource
    private SynchronousFactory syncFactory;
    @Autowired
    private OperaPswService operaPswService;


    /**
     * 同步配置
     * @param accountId     被新增区域id
     * @param addAccountId  复制此区域的配置
     * @param configType    配置类型-SynchronousConfigEnum
     * @return Result
     */
    @GetMapping("/copyConfig")
    public Result<String> copyConfig(Integer accountId, Integer addAccountId, String configType, String operaPsw) {
        /**操作密码校验*/
        operaPswService.checkOperaPsw(BaseController.getLogin(ServletUtils.getRequest()),
                    OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), operaPsw);

        List<Integer> configTypeList = Arrays.asList(configType.split(",")).stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        for (Integer config : configTypeList) {
            SynchronousConfigEnum configEnum = SynchronousConfigEnum.getByCode(config);
            if (Objects.isNull(configEnum)){
                return Result.error("同步配置不存在");
            }
            SynchronousService service = syncFactory.getService(configEnum);
            service.synchronousConfig(accountId, addAccountId);
        }
        return Result.success();
    }


}
